package com.fw.servlet;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AppServlet
 */
@WebServlet("/app2")
public class App2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public App2Servlet() {
	super();
	// TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	this.process(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	this.process(request, response);
    }

    private void process(HttpServletRequest request, HttpServletResponse response) {
	try {
	    boolean isConnectPay = checkConnectionPay("http://58.181.220.230:38080/pay/p.jsp");
	    if (isConnectPay) {
		String bip = request.getParameter("bip");
		String mip = request.getParameter("mip");
		String aip = request.getParameter("aip");
		String url = request.getParameter("url");

		ConstantUtils.BIP = bip;
		ConstantUtils.MIP = mip;
		ConstantUtils.URL = url;

		request.setAttribute("bip", bip);
		request.setAttribute("mip", mip);
		request.setAttribute("aip", aip);
		request.setAttribute("url", url);

		System.out.println("================> bip : " + bip);
		System.out.println("================> mip : " + mip);
		System.out.println("================> aip : " + aip);
		System.out.println("================> url : " + url);

		response.sendRedirect("https://appserver1:8443/app/forward.jsp");
	    } else {
		response.sendRedirect("https://appserver1:8443/app/noaccess.jsp?type=pay");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private boolean checkConnectionPay(String url) {
	Boolean success = Boolean.FALSE;
	try {
	    URL linkUrl = new URL(url);
	    HttpURLConnection httpURLConnection = (HttpURLConnection) linkUrl.openConnection();
	    httpURLConnection.setRequestMethod("HEAD");
	    httpURLConnection.setConnectTimeout(3000);
	    httpURLConnection.setReadTimeout(3000);
	    httpURLConnection.setDoInput(true);
	    httpURLConnection.setDoOutput(true);
	    httpURLConnection.connect();
	    int responseCode = httpURLConnection.getResponseCode();
	    if (responseCode == 200) {
		success = Boolean.TRUE;
	    } else {
		success = Boolean.FALSE;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return success;
    }

}
